/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.prp.gwt.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.IListener;
import com.prp.gwt.client.vo.User;

/**
 * The top panel, which contains the 'welcome' message and various links.
 */
public class TopPanel extends HorizontalPanel implements IWidget {

	private static TopPanel instance;
	private static Dictionary dictionary = Dictionary
			.getDictionary("constants");
	private HTML signOut;
	private HorizontalPanel innerPanel = new HorizontalPanel();
	private HorizontalPanel panel = new HorizontalPanel();
	private HTML root;
	private PopupPanel popup = new PopupPanel(true);
	private IListener listener;

	public static TopPanel getInstance() {
		if (instance == null)
			instance = new TopPanel();
		return instance;
	}

	private TopPanel() {
//		RootPanel.get("id").add(this);
		signOut = new HTML(dictionary.get("quit"));
		root = new HTML(dictionary.get("root"));
		addStyleName("topPanel");
		Image logo = new Image(dictionary.get("logo"));
		logo.addStyleName("topPanel-logo");
		add(logo);
		innerPanel.addStyleName("topPanel-bar");
		innerPanel.add(new HTML(dictionary.get("greetings")));
		innerPanel.setHorizontalAlignment(ALIGN_RIGHT);
		innerPanel.add(panel);
		add(innerPanel);
		setHorizontalAlignment(ALIGN_LEFT);
		signOut.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				listener.signOut();
			}

		});
		root.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				popup
						.setPopupPositionAndShow(new PopupPanel.PositionCallback() {

							@Override
							public void setPosition(int offsetWidth,
									int offsetHeight) {
								int left = root.getAbsoluteLeft();
								int top = root.getAbsoluteTop()
										+ root.getOffsetHeight();
								popup.setPopupPosition(left, top);

							}

						});
			}
		});
		popup.setWidget(getPopupWidget());
	}

	public void load(User usr) {
		if (usr == null)
			return;
		panel.addStyleName("topPanel-infoPanel");
		panel.clear();
		panel.add(new HTML(dictionary.get("hello") + usr.getUsername()));
		panel.add(new HTML(dictionary.get("interval")));
		panel.add(new HTML(dictionary.get("level")));
		switch (usr.getIdentity()) {
		case ClientConstants.USER:
			panel.add(new HTML(dictionary.get("user")));
			break;
		case ClientConstants.ADMIN:
			panel.add(new HTML(dictionary.get("administrator")));
			break;
		case ClientConstants.ROOT:
			panel.add(root);
			break;
		}
		panel.add(new HTML(dictionary.get("interval")));
		panel.add(signOut);
	}

	public VerticalPanel getPopupWidget() {
		VerticalPanel panel = new VerticalPanel();
		final HTML mode1 = new HTML(dictionary.get("adminmode"));
		final HTML mode2 = new HTML(dictionary.get("usermode"));
		mode1.addMouseOverHandler(new MouseOverHandler(){
			@Override
			public void onMouseOver(MouseOverEvent event) {
				mode1.addStyleName("mouseOver");
				mode2.removeStyleName("mouseOver");
			}
		});
		mode2.addMouseOverHandler(new MouseOverHandler(){
			@Override
			public void onMouseOver(MouseOverEvent event) {
				mode2.addStyleName("mouseOver");
				mode1.removeStyleName("mouseOver");
			}
		});
		panel.add(mode1);
		panel.add(mode2);
		mode1.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.replace(Host.getADMIN());
			}
		});
		mode2.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.replace(Host.getHOME());
			}
		});
		return panel;
	}

	@Override
	public void setListener(IListener listener) {
		this.listener = listener;
	}

	@Override
	public void callFunction(Object arg) {
		// do nothing
	}

}
